package com.aaa.sbms.service;

import com.aaa.sbms.dao.LeaveDao;
import com.aaa.sbms.util.ResultUtil;
import com.aaa.sbms.util.SessionUtil;
import com.aaa.sbms.util.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.*;

@Service
public class LeaveServiceImpl implements LeaveService {

    @Autowired(required = false)
    private LeaveDao leaveDao;

    @Override
    public Map depts() {
        Map resultMap = new HashMap();
        List<Map> list = leaveDao.depts();
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public Map leavePage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = leaveDao.leavePage(map);
        int total = leaveDao.leavePageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map leaderCheckPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = leaveDao.leaderCheckPage(map);
        int total = leaveDao.leaderCheckPageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map checkLeavePage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = leaveDao.checkLeavePage(map);
        int total = leaveDao.checkLeavePageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public ResultUtil leaveOne() {
        Map user = (Map) UserSession.getUser();
        int userid = Integer.valueOf(user.get("userid").toString());
        Map map = leaveDao.leaveOne(userid);
        if (map==null){
            return new ResultUtil(2002,"没有请假，可以请假",user);
        }
        return new ResultUtil(2001,"你已经请过假了",map);
    }

    @Override
    public Map personCheckOne(int id) {
        Map resultMap = new HashMap();
        Map list = leaveDao.personCheckOne(id);
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public Map leaderCheckOne(int id) {
        Map resultMap = new HashMap();
        Map list = leaveDao.leaderCheckOne(id);
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public int addLeave(Map map) {

        Map user = (Map) UserSession.getUser();
        int userid = Integer.valueOf(user.get("userid").toString());
        //查询有这个userId和未审核的，如果有，直接返回
        int firstCheckNo = leaveDao.isFirstCheckNo(userid);
        if (firstCheckNo>0){
            return 0;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //日期选择器获取的是String类型的
        String begintime = map.get("begintime").toString();
        String endtime = map.get("endtime").toString();

        String telephone=map.get("telephone").toString();
        map.put("phone",telephone);

        try {
            Date begintime1 = sdf.parse(begintime);
            Date endtime1 = sdf.parse(endtime);
            int days = (int)Math.ceil((endtime1.getTime()-begintime1.getTime())/(24*60*60*1000d)+1);
            Date begintime2 = sdf2.parse(begintime);
            Date endtime2 = sdf2.parse(endtime);
            map.put("begintime",begintime2);
            map.put("endtime",endtime2);
            map.put("leavedays",days);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return leaveDao.addLeave(map);
    }

    @Override
    public int updateFirstCheck(Map map) {
        return leaveDao.updateFirstCheck(map);
    }

    @Override
    public int updateSecondCheck(Map map) {
        return leaveDao.updateSecondCheck(map);
    }
}
